

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>saml2.utils &mdash; SAML Python library classes and methods</title>

    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />

    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="top" title="SAML Python library classes and methods" href="../../index.html" />
    <link rel="up" title="Class code" href="../index.html" />
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Class Index"
             >modules</a> |</li>
        <li><a href="../../index.html">SAML Python library classes and methods</a> &raquo;</li>
          <li><a href="../index.html" accesskey="U">Class code</a> &raquo;</li>
      </ul>
    </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">

  <h1>Source code for saml2.utils</h1><div class="highlight"><pre>
<span class="c"># -*- coding: utf-8 -*-</span>


<span class="kn">import</span> <span class="nn">base64</span>
<span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span>
<span class="kn">import</span> <span class="nn">calendar</span>
<span class="kn">from</span> <span class="nn">hashlib</span> <span class="kn">import</span> <span class="n">sha1</span>
<span class="kn">from</span> <span class="nn">isodate</span> <span class="kn">import</span> <span class="n">parse_duration</span> <span class="k">as</span> <span class="n">duration_parser</span>
<span class="kn">from</span> <span class="nn">lxml</span> <span class="kn">import</span> <span class="n">etree</span>
<span class="kn">from</span> <span class="nn">lxml.etree</span> <span class="kn">import</span> <span class="n">ElementBase</span>
<span class="kn">from</span> <span class="nn">os.path</span> <span class="kn">import</span> <span class="n">basename</span><span class="p">,</span> <span class="n">dirname</span><span class="p">,</span> <span class="n">join</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">from</span> <span class="nn">sys</span> <span class="kn">import</span> <span class="n">stderr</span>
<span class="kn">from</span> <span class="nn">tempfile</span> <span class="kn">import</span> <span class="n">NamedTemporaryFile</span>
<span class="kn">from</span> <span class="nn">textwrap</span> <span class="kn">import</span> <span class="n">wrap</span>
<span class="kn">from</span> <span class="nn">urllib</span> <span class="kn">import</span> <span class="n">quote_plus</span>
<span class="kn">from</span> <span class="nn">uuid</span> <span class="kn">import</span> <span class="n">uuid4</span>
<span class="kn">from</span> <span class="nn">xml.dom.minidom</span> <span class="kn">import</span> <span class="n">Document</span><span class="p">,</span> <span class="n">parseString</span><span class="p">,</span> <span class="n">Element</span>
<span class="kn">from</span> <span class="nn">xml.etree.ElementTree</span> <span class="kn">import</span> <span class="n">tostring</span>
<span class="kn">import</span> <span class="nn">zlib</span>

<span class="kn">import</span> <span class="nn">dm.xmlsec.binding</span> <span class="kn">as</span> <span class="nn">xmlsec</span>
<span class="kn">from</span> <span class="nn">dm.xmlsec.binding.tmpl</span> <span class="kn">import</span> <span class="n">EncData</span><span class="p">,</span> <span class="n">Signature</span>
<span class="kn">from</span> <span class="nn">M2Crypto</span> <span class="kn">import</span> <span class="n">X509</span>

<span class="kn">from</span> <span class="nn">saml2.constants</span> <span class="kn">import</span> <span class="n">OneLogin_Saml2_Constants</span>
<span class="kn">from</span> <span class="nn">saml2.errors</span> <span class="kn">import</span> <span class="n">OneLogin_Saml2_Error</span>


<span class="k">def</span> <span class="nf">_</span><span class="p">(</span><span class="n">msg</span><span class="p">):</span>
    <span class="c"># Fixme Add i18n support</span>
    <span class="k">return</span> <span class="n">msg</span>


<div class="viewcode-block" id="OneLogin_Saml2_Utils"><a class="viewcode-back" href="../../saml2.html#saml2.utils.OneLogin_Saml2_Utils">[docs]</a><span class="k">class</span> <span class="nc">OneLogin_Saml2_Utils</span><span class="p">:</span>

    <span class="nd">@staticmethod</span>
<div class="viewcode-block" id="OneLogin_Saml2_Utils.decode_base64_and_inflate"><a class="viewcode-back" href="../../saml2.html#saml2.utils.OneLogin_Saml2_Utils.decode_base64_and_inflate">[docs]</a>    <span class="k">def</span> <span class="nf">decode_base64_and_inflate</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; base64 decodes and then inflates according to RFC1951</span>
<span class="sd">        :param value: a deflated and encoded string</span>
<span class="sd">        :return: the string after decoding and inflating</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="n">zlib</span><span class="o">.</span><span class="n">decompress</span><span class="p">(</span><span class="n">base64</span><span class="o">.</span><span class="n">b64decode</span><span class="p">(</span><span class="n">value</span><span class="p">),</span> <span class="o">-</span><span class="mi">15</span><span class="p">)</span>
</div>
    <span class="nd">@staticmethod</span>
<div class="viewcode-block" id="OneLogin_Saml2_Utils.deflate_and_base64_encode"><a class="viewcode-back" href="../../saml2.html#saml2.utils.OneLogin_Saml2_Utils.deflate_and_base64_encode">[docs]</a>    <span class="k">def</span> <span class="nf">deflate_and_base64_encode</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Deflates and the base64 encodes a string</span>
<span class="sd">        :param value: The string to deflate and encode</span>
<span class="sd">        :return: The deflated and encoded string</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">base64</span><span class="o">.</span><span class="n">b64encode</span><span class="p">(</span><span class="n">zlib</span><span class="o">.</span><span class="n">compress</span><span class="p">(</span><span class="n">value</span><span class="p">)[</span><span class="mi">2</span><span class="p">:</span><span class="o">-</span><span class="mi">4</span><span class="p">])</span>
</div>
    <span class="nd">@staticmethod</span>
<div class="viewcode-block" id="OneLogin_Saml2_Utils.validate_xml"><a class="viewcode-back" href="../../saml2.html#saml2.utils.OneLogin_Saml2_Utils.validate_xml">[docs]</a>    <span class="k">def</span> <span class="nf">validate_xml</span><span class="p">(</span><span class="n">xml</span><span class="p">,</span> <span class="n">schema</span><span class="p">,</span> <span class="n">debug</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">assert</span> <span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">xml</span><span class="p">,</span> <span class="nb">basestring</span><span class="p">)</span> <span class="ow">or</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">xml</span><span class="p">,</span> <span class="n">Document</span><span class="p">))</span>
        <span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">schema</span><span class="p">,</span> <span class="nb">basestring</span><span class="p">)</span>

        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">xml</span><span class="p">,</span> <span class="n">Document</span><span class="p">):</span>
            <span class="n">xml</span> <span class="o">=</span> <span class="n">xml</span><span class="o">.</span><span class="n">toxml</span><span class="p">()</span>

        <span class="c"># Switch to lxml for schema validation</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">dom</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">fromstring</span><span class="p">(</span><span class="n">xml</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
            <span class="k">return</span> <span class="s">&#39;unloaded_xml&#39;</span>

        <span class="n">schema_file</span> <span class="o">=</span> <span class="n">join</span><span class="p">(</span><span class="n">dirname</span><span class="p">(</span><span class="n">__file__</span><span class="p">),</span> <span class="s">&#39;schemas&#39;</span><span class="p">,</span> <span class="n">schema</span><span class="p">)</span>
        <span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">schema_file</span><span class="p">,</span> <span class="s">&#39;r&#39;</span><span class="p">)</span>
        <span class="n">schema_doc</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
        <span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
        <span class="n">xmlschema</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">XMLSchema</span><span class="p">(</span><span class="n">schema_doc</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">xmlschema</span><span class="o">.</span><span class="n">validate</span><span class="p">(</span><span class="n">dom</span><span class="p">):</span>
            <span class="n">xml_errors</span> <span class="o">=</span> <span class="p">[</span><span class="n">xmlschema</span><span class="o">.</span><span class="n">error_log</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">debug</span><span class="p">:</span>
                <span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&#39;Errors validating the metadata&#39;</span><span class="p">)</span>
                <span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&#39;:</span><span class="se">\n\n</span><span class="s">&#39;</span><span class="p">)</span>
                <span class="k">for</span> <span class="n">error</span> <span class="ow">in</span> <span class="n">xml_errors</span><span class="p">:</span>
                    <span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="se">\n</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">error</span><span class="o">.</span><span class="n">message</span><span class="p">)</span>

            <span class="k">return</span> <span class="s">&#39;invalid_xml&#39;</span>

        <span class="k">return</span> <span class="n">parseString</span><span class="p">(</span><span class="n">etree</span><span class="o">.</span><span class="n">tostring</span><span class="p">(</span><span class="n">dom</span><span class="p">))</span>
</div>
    <span class="nd">@staticmethod</span>
<div class="viewcode-block" id="OneLogin_Saml2_Utils.format_cert"><a class="viewcode-back" href="../../saml2.html#saml2.utils.OneLogin_Saml2_Utils.format_cert">[docs]</a>    <span class="k">def</span> <span class="nf">format_cert</span><span class="p">(</span><span class="n">cert</span><span class="p">,</span> <span class="n">heads</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a x509 cert (adding header &amp; footer if required).</span>

<span class="sd">        :param cert: A x509 unformated cert</span>
<span class="sd">        :type: string</span>

<span class="sd">        :param heads: True if we want to include head and footer</span>
<span class="sd">        :type: boolean</span>

<span class="sd">        :returns: Formated cert</span>
<span class="sd">        :rtype: string</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">x509_cert</span> <span class="o">=</span> <span class="n">cert</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;</span><span class="se">\x0D</span><span class="s">&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">)</span>
        <span class="n">x509_cert</span> <span class="o">=</span> <span class="n">x509_cert</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;</span><span class="se">\r</span><span class="s">&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">)</span>
        <span class="n">x509_cert</span> <span class="o">=</span> <span class="n">x509_cert</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">x509_cert</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">x509_cert</span> <span class="o">=</span> <span class="n">x509_cert</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;-----BEGIN CERTIFICATE-----&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">)</span>
            <span class="n">x509_cert</span> <span class="o">=</span> <span class="n">x509_cert</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;-----END CERTIFICATE-----&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">)</span>
            <span class="n">x509_cert</span> <span class="o">=</span> <span class="n">x509_cert</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39; &#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">)</span>

            <span class="k">if</span> <span class="n">heads</span><span class="p">:</span>
                <span class="n">x509_cert</span> <span class="o">=</span> <span class="s">&#39;-----BEGIN CERTIFICATE-----</span><span class="se">\n</span><span class="s">&#39;</span> <span class="o">+</span> <span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">wrap</span><span class="p">(</span><span class="n">x509_cert</span><span class="p">,</span> <span class="mi">64</span><span class="p">))</span> <span class="o">+</span> <span class="s">&#39;</span><span class="se">\n</span><span class="s">-----END CERTIFICATE-----</span><span class="se">\n</span><span class="s">&#39;</span>

        <span class="k">return</span> <span class="n">x509_cert</span>
</div>
    <span class="nd">@staticmethod</span>
<div class="viewcode-block" id="OneLogin_Saml2_Utils.redirect"><a class="viewcode-back" href="../../saml2.html#saml2.utils.OneLogin_Saml2_Utils.redirect">[docs]</a>    <span class="k">def</span> <span class="nf">redirect</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">parameters</span><span class="o">=</span><span class="p">{},</span> <span class="n">request_data</span><span class="o">=</span><span class="p">{}):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Executes a redirection to the provided url (or return the target url).</span>

<span class="sd">        :param url: The target url</span>
<span class="sd">        :type: string</span>

<span class="sd">        :param parameters: Extra parameters to be passed as part of the url</span>
<span class="sd">        :type: dict</span>

<span class="sd">        :param request_data: The request as a dict</span>
<span class="sd">        :type: dict</span>

<span class="sd">        :returns: Url</span>
<span class="sd">        :rtype: string</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="nb">basestring</span><span class="p">)</span>
        <span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">parameters</span><span class="p">,</span> <span class="nb">dict</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">url</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&#39;/&#39;</span><span class="p">):</span>
            <span class="n">url</span> <span class="o">=</span> <span class="s">&#39;</span><span class="si">%s%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">OneLogin_Saml2_Utils</span><span class="o">.</span><span class="n">get_self_url_host</span><span class="p">(</span><span class="n">request_data</span><span class="p">),</span> <span class="n">url</span><span class="p">)</span>

        <span class="c"># Verify that the URL is to a http or https site.</span>
        <span class="k">if</span> <span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="s">&#39;^https?://&#39;</span><span class="p">,</span> <span class="n">url</span><span class="p">)</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">OneLogin_Saml2_Error</span><span class="p">(</span>
                <span class="s">&#39;Redirect to invalid URL: &#39;</span> <span class="o">+</span> <span class="n">url</span><span class="p">,</span>
                <span class="n">OneLogin_Saml2_Error</span><span class="o">.</span><span class="n">REDIRECT_INVALID_URL</span>
            <span class="p">)</span>

        <span class="c"># Add encoded parameters</span>
        <span class="k">if</span> <span class="n">url</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s">&#39;?&#39;</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">param_prefix</span> <span class="o">=</span> <span class="s">&#39;?&#39;</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">param_prefix</span> <span class="o">=</span> <span class="s">&#39;&amp;&#39;</span>

        <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">parameters</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>

            <span class="k">if</span> <span class="n">value</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
                <span class="n">param</span> <span class="o">=</span> <span class="n">urlencode</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
            <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
                <span class="n">param</span> <span class="o">=</span> <span class="s">&#39;&#39;</span>
                <span class="k">for</span> <span class="n">val</span> <span class="ow">in</span> <span class="n">value</span><span class="p">:</span>
                    <span class="n">param</span> <span class="o">+=</span> <span class="n">quote_plus</span><span class="p">(</span><span class="n">name</span><span class="p">)</span> <span class="o">+</span> <span class="s">&#39;[]=&#39;</span> <span class="o">+</span> <span class="n">quote_plus</span><span class="p">(</span><span class="n">val</span><span class="p">)</span> <span class="o">+</span> <span class="s">&#39;&amp;&#39;</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">param</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">param</span> <span class="o">=</span> <span class="n">param</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">param</span> <span class="o">=</span> <span class="n">quote_plus</span><span class="p">(</span><span class="n">name</span><span class="p">)</span> <span class="o">+</span> <span class="s">&#39;=&#39;</span> <span class="o">+</span> <span class="n">quote_plus</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>

            <span class="n">url</span> <span class="o">+=</span> <span class="n">param_prefix</span> <span class="o">+</span> <span class="n">param</span>
            <span class="n">param_prefix</span> <span class="o">=</span> <span class="s">&#39;&amp;&#39;</span>

        <span class="k">return</span> <span class="n">url</span>
</div>
    <span class="nd">@staticmethod</span>
<div class="viewcode-block" id="OneLogin_Saml2_Utils.get_self_url_host"><a class="viewcode-back" href="../../saml2.html#saml2.utils.OneLogin_Saml2_Utils.get_self_url_host">[docs]</a>    <span class="k">def</span> <span class="nf">get_self_url_host</span><span class="p">(</span><span class="n">request_data</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the protocol + the current host + the port (if different than</span>
<span class="sd">        common ports).</span>

<span class="sd">        :param request_data: The request as a dict</span>
<span class="sd">        :type: dict</span>

<span class="sd">        :return: Url</span>
<span class="sd">        :rtype: string</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">current_host</span> <span class="o">=</span> <span class="n">OneLogin_Saml2_Utils</span><span class="o">.</span><span class="n">get_self_host</span><span class="p">(</span><span class="n">request_data</span><span class="p">)</span>
        <span class="n">port</span> <span class="o">=</span> <span class="s">&#39;&#39;</span>
        <span class="k">if</span> <span class="n">OneLogin_Saml2_Utils</span><span class="o">.</span><span class="n">is_https</span><span class="p">(</span><span class="n">request_data</span><span class="p">):</span>
            <span class="n">protocol</span> <span class="o">=</span> <span class="s">&#39;https&#39;</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">protocol</span> <span class="o">=</span> <span class="s">&#39;http&#39;</span>

        <span class="k">if</span> <span class="s">&#39;server_port&#39;</span> <span class="ow">in</span> <span class="n">request_data</span><span class="p">:</span>
            <span class="n">port_number</span> <span class="o">=</span> <span class="n">request_data</span><span class="p">[</span><span class="s">&#39;server_port&#39;</span><span class="p">]</span>
            <span class="n">port</span> <span class="o">=</span> <span class="s">&#39;:&#39;</span> <span class="o">+</span> <span class="n">port_number</span>

            <span class="k">if</span> <span class="n">protocol</span> <span class="o">==</span> <span class="s">&#39;http&#39;</span> <span class="ow">and</span> <span class="n">port_number</span> <span class="o">==</span> <span class="s">&#39;80&#39;</span><span class="p">:</span>
                <span class="n">port</span> <span class="o">=</span> <span class="s">&#39;&#39;</span>
            <span class="k">elif</span> <span class="n">protocol</span> <span class="o">==</span> <span class="s">&#39;https&#39;</span> <span class="ow">and</span> <span class="n">port_number</span> <span class="o">==</span> <span class="s">&#39;443&#39;</span><span class="p">:</span>
                <span class="n">port</span> <span class="o">=</span> <span class="s">&#39;&#39;</span>

        <span class="k">return</span> <span class="s">&#39;</span><span class="si">%s</span><span class="s">://</span><span class="si">%s%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">protocol</span><span class="p">,</span> <span class="n">current_host</span><span class="p">,</span> <span class="n">port</span><span class="p">)</span>
</div>
    <span class="nd">@staticmethod</span>
<div class="viewcode-block" id="OneLogin_Saml2_Utils.get_self_host"><a class="viewcode-back" href="../../saml2.html#saml2.utils.OneLogin_Saml2_Utils.get_self_host">[docs]</a>    <span class="k">def</span> <span class="nf">get_self_host</span><span class="p">(</span><span class="n">request_data</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the current host.</span>

<span class="sd">        :param request_data: The request as a dict</span>
<span class="sd">        :type: dict</span>

<span class="sd">        :return: The current host</span>
<span class="sd">        :rtype: string</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="s">&#39;http_host&#39;</span> <span class="ow">in</span> <span class="n">request_data</span><span class="p">:</span>
            <span class="n">current_host</span> <span class="o">=</span> <span class="n">request_data</span><span class="p">[</span><span class="s">&#39;http_host&#39;</span><span class="p">]</span>
        <span class="k">elif</span> <span class="s">&#39;server_name&#39;</span> <span class="ow">in</span> <span class="n">request_data</span><span class="p">:</span>
            <span class="n">current_host</span> <span class="o">=</span> <span class="n">request_data</span><span class="p">[</span><span class="s">&#39;server_name&#39;</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s">&#39;No hostname defined&#39;</span><span class="p">)</span>

        <span class="k">if</span> <span class="s">&#39;:&#39;</span> <span class="ow">in</span> <span class="n">current_host</span><span class="p">:</span>
            <span class="n">current_host_data</span> <span class="o">=</span> <span class="n">current_host</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;:&#39;</span><span class="p">)</span>
            <span class="n">possible_port</span> <span class="o">=</span> <span class="n">current_host_data</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">possible_port</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">possible_port</span><span class="p">)</span>
                <span class="n">current_host</span> <span class="o">=</span> <span class="n">current_host_data</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
            <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                <span class="n">current_host</span> <span class="o">=</span> <span class="s">&#39;:&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">current_host_data</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">current_host</span>
</div>
    <span class="nd">@staticmethod</span>
<div class="viewcode-block" id="OneLogin_Saml2_Utils.is_https"><a class="viewcode-back" href="../../saml2.html#saml2.utils.OneLogin_Saml2_Utils.is_https">[docs]</a>    <span class="k">def</span> <span class="nf">is_https</span><span class="p">(</span><span class="n">request_data</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Checks if https or http.</span>

<span class="sd">        :param request_data: The request as a dict</span>
<span class="sd">        :type: dict</span>

<span class="sd">        :return: False if https is not active</span>
<span class="sd">        :rtype: boolean</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">is_https</span> <span class="o">=</span> <span class="s">&#39;https&#39;</span> <span class="ow">in</span> <span class="n">request_data</span> <span class="ow">and</span> <span class="n">request_data</span><span class="p">[</span><span class="s">&#39;https&#39;</span><span class="p">]</span> <span class="o">!=</span> <span class="s">&#39;off&#39;</span>
        <span class="n">is_https</span> <span class="o">=</span> <span class="n">is_https</span> <span class="ow">or</span> <span class="p">(</span><span class="s">&#39;server_port&#39;</span> <span class="ow">in</span> <span class="n">request_data</span> <span class="ow">and</span> <span class="n">request_data</span><span class="p">[</span><span class="s">&#39;server_port&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="s">&#39;443&#39;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">is_https</span>
</div>
    <span class="nd">@staticmethod</span>
<div class="viewcode-block" id="OneLogin_Saml2_Utils.get_self_url_no_query"><a class="viewcode-back" href="../../saml2.html#saml2.utils.OneLogin_Saml2_Utils.get_self_url_no_query">[docs]</a>    <span class="k">def</span> <span class="nf">get_self_url_no_query</span><span class="p">(</span><span class="n">request_data</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the URL of the current host + current view.</span>

<span class="sd">        :param request_data: The request as a dict</span>
<span class="sd">        :type: dict</span>

<span class="sd">        :return: The url of current host + current view</span>
<span class="sd">        :rtype: string</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">self_url_host</span> <span class="o">=</span> <span class="n">OneLogin_Saml2_Utils</span><span class="o">.</span><span class="n">get_self_url_host</span><span class="p">(</span><span class="n">request_data</span><span class="p">)</span>
        <span class="n">script_name</span> <span class="o">=</span> <span class="n">request_data</span><span class="p">[</span><span class="s">&#39;script_name&#39;</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">script_name</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">!=</span> <span class="s">&#39;/&#39;</span><span class="p">:</span>
            <span class="n">script_name</span> <span class="o">=</span> <span class="s">&#39;/&#39;</span> <span class="o">+</span> <span class="n">script_name</span>
        <span class="n">self_url_host</span> <span class="o">+=</span> <span class="n">script_name</span>
        <span class="k">if</span> <span class="s">&#39;path_info&#39;</span> <span class="ow">in</span> <span class="n">request_data</span><span class="p">:</span>
            <span class="n">self_url_host</span> <span class="o">+=</span> <span class="n">request_data</span><span class="p">[</span><span class="s">&#39;path_info&#39;</span><span class="p">]</span>

        <span class="k">return</span> <span class="n">self_url_host</span>
</div>
    <span class="nd">@staticmethod</span>
<div class="viewcode-block" id="OneLogin_Saml2_Utils.get_self_url"><a class="viewcode-back" href="../../saml2.html#saml2.utils.OneLogin_Saml2_Utils.get_self_url">[docs]</a>    <span class="k">def</span> <span class="nf">get_self_url</span><span class="p">(</span><span class="n">request_data</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the URL of the current host + current view + query.</span>

<span class="sd">        :param request_data: The request as a dict</span>
<span class="sd">        :type: dict</span>

<span class="sd">        :return: The url of current host + current view + query</span>
<span class="sd">        :rtype: string</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">self_url_host</span> <span class="o">=</span> <span class="n">OneLogin_Saml2_Utils</span><span class="o">.</span><span class="n">get_self_url_host</span><span class="p">(</span><span class="n">request_data</span><span class="p">)</span>

        <span class="n">request_uri</span> <span class="o">=</span> <span class="s">&#39;&#39;</span>
        <span class="k">if</span> <span class="s">&#39;request_uri&#39;</span> <span class="ow">in</span> <span class="n">request_data</span><span class="p">:</span>
            <span class="n">request_uri</span> <span class="o">=</span> <span class="n">request_data</span><span class="p">[</span><span class="s">&#39;request_uri&#39;</span><span class="p">]</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">request_uri</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&#39;/&#39;</span><span class="p">):</span>
                <span class="n">match</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="s">&#39;^https?://[^/]*(/.*)&#39;</span><span class="p">,</span> <span class="n">request_uri</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">match</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
                    <span class="n">request_uri</span> <span class="o">=</span> <span class="n">match</span><span class="o">.</span><span class="n">groups</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>

        <span class="k">return</span> <span class="n">self_url_host</span> <span class="o">+</span> <span class="n">request_uri</span>
</div>
    <span class="nd">@staticmethod</span>
<div class="viewcode-block" id="OneLogin_Saml2_Utils.generate_unique_id"><a class="viewcode-back" href="../../saml2.html#saml2.utils.OneLogin_Saml2_Utils.generate_unique_id">[docs]</a>    <span class="k">def</span> <span class="nf">generate_unique_id</span><span class="p">():</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Generates an unique string (used for example as ID for assertions).</span>

<span class="sd">        :return: A unique string</span>
<span class="sd">        :rtype: string</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="s">&#39;ONELOGIN_</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">sha1</span><span class="p">(</span><span class="n">uuid4</span><span class="p">()</span><span class="o">.</span><span class="n">hex</span><span class="p">)</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>
</div>
    <span class="nd">@staticmethod</span>
<div class="viewcode-block" id="OneLogin_Saml2_Utils.parse_time_to_SAML"><a class="viewcode-back" href="../../saml2.html#saml2.utils.OneLogin_Saml2_Utils.parse_time_to_SAML">[docs]</a>    <span class="k">def</span> <span class="nf">parse_time_to_SAML</span><span class="p">(</span><span class="n">time</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Converts a UNIX timestamp to SAML2 timestamp on the form</span>
<span class="sd">        yyyy-mm-ddThh:mm:ss(\.s+)?Z.</span>

<span class="sd">        :param time: The time we should convert (DateTime).</span>
<span class="sd">        :type: string</span>

<span class="sd">        :return: SAML2 timestamp.</span>
<span class="sd">        :rtype: string</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">data</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">utcfromtimestamp</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">time</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">data</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s">&#39;%Y-%m-</span><span class="si">%d</span><span class="s">T%H:%M:%SZ&#39;</span><span class="p">)</span>
</div>
    <span class="nd">@staticmethod</span>
<div class="viewcode-block" id="OneLogin_Saml2_Utils.parse_SAML_to_time"><a class="viewcode-back" href="../../saml2.html#saml2.utils.OneLogin_Saml2_Utils.parse_SAML_to_time">[docs]</a>    <span class="k">def</span> <span class="nf">parse_SAML_to_time</span><span class="p">(</span><span class="n">timestr</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Converts a SAML2 timestamp on the form yyyy-mm-ddThh:mm:ss(\.s+)?Z</span>
<span class="sd">        to a UNIX timestamp. The sub-second part is ignored.</span>

<span class="sd">        :param time: The time we should convert (SAML Timestamp).</span>
<span class="sd">        :type: string</span>

<span class="sd">        :return: Converted to a unix timestamp.</span>
<span class="sd">        :rtype: int</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">strptime</span><span class="p">(</span><span class="n">timestr</span><span class="p">,</span> <span class="s">&#39;%Y-%m-</span><span class="si">%d</span><span class="s">T%H:%M:%SZ&#39;</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">strptime</span><span class="p">(</span><span class="n">timestr</span><span class="p">,</span> <span class="s">&#39;%Y-%m-</span><span class="si">%d</span><span class="s">T%H:%M:%S.</span><span class="si">%f</span><span class="s">Z&#39;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">calendar</span><span class="o">.</span><span class="n">timegm</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">utctimetuple</span><span class="p">())</span>
</div>
    <span class="nd">@staticmethod</span>
<div class="viewcode-block" id="OneLogin_Saml2_Utils.parse_duration"><a class="viewcode-back" href="../../saml2.html#saml2.utils.OneLogin_Saml2_Utils.parse_duration">[docs]</a>    <span class="k">def</span> <span class="nf">parse_duration</span><span class="p">(</span><span class="n">duration</span><span class="p">,</span> <span class="n">timestamp</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Interprets a ISO8601 duration value relative to a given timestamp.</span>

<span class="sd">        :param duration: The duration, as a string.</span>
<span class="sd">        :type: string</span>

<span class="sd">        :param timestamp: The unix timestamp we should apply the duration to.</span>
<span class="sd">                          Optional, default to the current time.</span>
<span class="sd">        :type: string</span>

<span class="sd">        :return: The new timestamp, after the duration is applied.</span>
<span class="sd">        :rtype: int</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">duration</span><span class="p">,</span> <span class="nb">basestring</span><span class="p">)</span>
        <span class="k">assert</span> <span class="p">(</span><span class="n">timestamp</span> <span class="ow">is</span> <span class="bp">None</span> <span class="ow">or</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">timestamp</span><span class="p">,</span> <span class="nb">int</span><span class="p">))</span>

        <span class="n">timedelta</span> <span class="o">=</span> <span class="n">duration_parser</span><span class="p">(</span><span class="n">duration</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">timestamp</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">utcnow</span><span class="p">()</span> <span class="o">+</span> <span class="n">timedelta</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">utcfromtimestamp</span><span class="p">(</span><span class="n">timestamp</span><span class="p">)</span> <span class="o">+</span> <span class="n">timedelta</span>
        <span class="k">return</span> <span class="n">calendar</span><span class="o">.</span><span class="n">timegm</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">utctimetuple</span><span class="p">())</span>
</div>
    <span class="nd">@staticmethod</span>
<div class="viewcode-block" id="OneLogin_Saml2_Utils.get_expire_time"><a class="viewcode-back" href="../../saml2.html#saml2.utils.OneLogin_Saml2_Utils.get_expire_time">[docs]</a>    <span class="k">def</span> <span class="nf">get_expire_time</span><span class="p">(</span><span class="n">cache_duration</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">valid_until</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Compares 2 dates and returns the earliest.</span>

<span class="sd">        :param cache_duration: The duration, as a string.</span>
<span class="sd">        :type: string</span>

<span class="sd">        :param valid_until: The valid until date, as a string or as a timestamp</span>
<span class="sd">        :type: string</span>

<span class="sd">        :return: The expiration time.</span>
<span class="sd">        :rtype: int</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">expire_time</span> <span class="o">=</span> <span class="bp">None</span>

        <span class="k">if</span> <span class="n">cache_duration</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">expire_time</span> <span class="o">=</span> <span class="n">OneLogin_Saml2_Utils</span><span class="o">.</span><span class="n">parse_duration</span><span class="p">(</span><span class="n">cache_duration</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">valid_until</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">valid_until</span><span class="p">,</span> <span class="nb">int</span><span class="p">):</span>
                <span class="n">valid_until_time</span> <span class="o">=</span> <span class="n">valid_until</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">valid_until_time</span> <span class="o">=</span> <span class="n">OneLogin_Saml2_Utils</span><span class="o">.</span><span class="n">parse_SAML_to_time</span><span class="p">(</span><span class="n">valid_until</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">expire_time</span> <span class="ow">is</span> <span class="bp">None</span> <span class="ow">or</span> <span class="n">expire_time</span> <span class="o">&gt;</span> <span class="n">valid_until_time</span><span class="p">:</span>
                <span class="n">expire_time</span> <span class="o">=</span> <span class="n">valid_until_time</span>

        <span class="k">if</span> <span class="n">expire_time</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="s">&#39;</span><span class="si">%d</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">expire_time</span>
        <span class="k">return</span> <span class="bp">None</span>
</div>
    <span class="nd">@staticmethod</span>
<div class="viewcode-block" id="OneLogin_Saml2_Utils.query"><a class="viewcode-back" href="../../saml2.html#saml2.utils.OneLogin_Saml2_Utils.query">[docs]</a>    <span class="k">def</span> <span class="nf">query</span><span class="p">(</span><span class="n">dom</span><span class="p">,</span> <span class="n">query</span><span class="p">,</span> <span class="n">context</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Extracts nodes that match the query from the Element</span>

<span class="sd">        :param dom: The root of the lxml objet</span>
<span class="sd">        :type: Element</span>

<span class="sd">        :param query: Xpath Expresion</span>
<span class="sd">        :type: string</span>

<span class="sd">        :param context: Context Node</span>
<span class="sd">        :type: DOMElement</span>

<span class="sd">        :returns: The queried nodes</span>
<span class="sd">        :rtype: list</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">context</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">dom</span><span class="o">.</span><span class="n">xpath</span><span class="p">(</span><span class="n">query</span><span class="p">,</span> <span class="n">namespaces</span><span class="o">=</span><span class="n">OneLogin_Saml2_Constants</span><span class="o">.</span><span class="n">NSMAP</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">context</span><span class="o">.</span><span class="n">xpath</span><span class="p">(</span><span class="n">query</span><span class="p">,</span> <span class="n">namespaces</span><span class="o">=</span><span class="n">OneLogin_Saml2_Constants</span><span class="o">.</span><span class="n">NSMAP</span><span class="p">)</span>
</div>
    <span class="nd">@staticmethod</span>
<div class="viewcode-block" id="OneLogin_Saml2_Utils.delete_local_session"><a class="viewcode-back" href="../../saml2.html#saml2.utils.OneLogin_Saml2_Utils.delete_local_session">[docs]</a>    <span class="k">def</span> <span class="nf">delete_local_session</span><span class="p">(</span><span class="n">callback</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Deletes the local session.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="n">callback</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">callback</span><span class="p">()</span>
</div>
    <span class="nd">@staticmethod</span>
<div class="viewcode-block" id="OneLogin_Saml2_Utils.calculate_x509_fingerprint"><a class="viewcode-back" href="../../saml2.html#saml2.utils.OneLogin_Saml2_Utils.calculate_x509_fingerprint">[docs]</a>    <span class="k">def</span> <span class="nf">calculate_x509_fingerprint</span><span class="p">(</span><span class="n">x509_cert</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Calculates the fingerprint of a x509cert.</span>

<span class="sd">        :param x509_cert: x509 cert</span>
<span class="sd">        :type: string</span>

<span class="sd">        :returns: Formated fingerprint</span>
<span class="sd">        :rtype: string</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">x509_cert</span><span class="p">,</span> <span class="nb">basestring</span><span class="p">)</span>

        <span class="n">lines</span> <span class="o">=</span> <span class="n">x509_cert</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</span><span class="p">)</span>
        <span class="n">data</span> <span class="o">=</span> <span class="s">&#39;&#39;</span>

        <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">lines</span><span class="p">:</span>
            <span class="c"># Remove &#39;\r&#39; from end of line if present.</span>
            <span class="n">line</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">rstrip</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">line</span> <span class="o">==</span> <span class="s">&#39;-----BEGIN CERTIFICATE-----&#39;</span><span class="p">:</span>
                <span class="c"># Delete junk from before the certificate.</span>
                <span class="n">data</span> <span class="o">=</span> <span class="s">&#39;&#39;</span>
            <span class="k">elif</span> <span class="n">line</span> <span class="o">==</span> <span class="s">&#39;-----END CERTIFICATE-----&#39;</span><span class="p">:</span>
                <span class="c"># Ignore data after the certificate.</span>
                <span class="k">break</span>
            <span class="k">elif</span> <span class="n">line</span> <span class="o">==</span> <span class="s">&#39;-----BEGIN PUBLIC KEY-----&#39;</span> <span class="ow">or</span> <span class="n">line</span> <span class="o">==</span> <span class="s">&#39;-----BEGIN RSA PRIVATE KEY-----&#39;</span><span class="p">:</span>
                <span class="c"># This isn&#39;t an X509 certificate.</span>
                <span class="k">return</span> <span class="bp">None</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c"># Append the current line to the certificate data.</span>
                <span class="n">data</span> <span class="o">+=</span> <span class="n">line</span>
        <span class="c"># &quot;data&quot; now contains the certificate as a base64-encoded string. The</span>
        <span class="c"># fingerprint of the certificate is the sha1-hash of the certificate.</span>
        <span class="k">return</span> <span class="n">sha1</span><span class="p">(</span><span class="n">base64</span><span class="o">.</span><span class="n">b64decode</span><span class="p">(</span><span class="n">data</span><span class="p">))</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
</div>
    <span class="nd">@staticmethod</span>
<div class="viewcode-block" id="OneLogin_Saml2_Utils.format_finger_print"><a class="viewcode-back" href="../../saml2.html#saml2.utils.OneLogin_Saml2_Utils.format_finger_print">[docs]</a>    <span class="k">def</span> <span class="nf">format_finger_print</span><span class="p">(</span><span class="n">fingerprint</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Formates a fingerprint.</span>

<span class="sd">        :param fingerprint: fingerprint</span>
<span class="sd">        :type: string</span>

<span class="sd">        :returns: Formated fingerprint</span>
<span class="sd">        :rtype: string</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">formated_fingerprint</span> <span class="o">=</span> <span class="n">fingerprint</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;:&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">formated_fingerprint</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
</div>
    <span class="nd">@staticmethod</span>
<div class="viewcode-block" id="OneLogin_Saml2_Utils.generate_name_id"><a class="viewcode-back" href="../../saml2.html#saml2.utils.OneLogin_Saml2_Utils.generate_name_id">[docs]</a>    <span class="k">def</span> <span class="nf">generate_name_id</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">sp_nq</span><span class="p">,</span> <span class="n">sp_format</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Generates a nameID.</span>

<span class="sd">        :param value: fingerprint</span>
<span class="sd">        :type: string</span>

<span class="sd">        :param sp_nq: SP Name Qualifier</span>
<span class="sd">        :type: string</span>

<span class="sd">        :param sp_format: SP Format</span>
<span class="sd">        :type: string</span>

<span class="sd">        :param key: SP Key to encrypt the nameID</span>
<span class="sd">        :type: string</span>

<span class="sd">        :returns: DOMElement | XMLSec nameID</span>
<span class="sd">        :rtype: string</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">doc</span> <span class="o">=</span> <span class="n">Document</span><span class="p">()</span>

        <span class="n">name_id</span> <span class="o">=</span> <span class="n">doc</span><span class="o">.</span><span class="n">createElement</span><span class="p">(</span><span class="s">&#39;saml:NameID&#39;</span><span class="p">)</span>
        <span class="n">name_id</span><span class="o">.</span><span class="n">setAttribute</span><span class="p">(</span><span class="s">&#39;SPNameQualifier&#39;</span><span class="p">,</span> <span class="n">sp_nq</span><span class="p">)</span>
        <span class="n">name_id</span><span class="o">.</span><span class="n">setAttribute</span><span class="p">(</span><span class="s">&#39;Format&#39;</span><span class="p">,</span> <span class="n">sp_format</span><span class="p">)</span>
        <span class="n">name_id</span><span class="o">.</span><span class="n">appendChild</span><span class="p">(</span><span class="n">doc</span><span class="o">.</span><span class="n">createTextNode</span><span class="p">(</span><span class="n">value</span><span class="p">))</span>

        <span class="n">doc</span><span class="o">.</span><span class="n">appendChild</span><span class="p">(</span><span class="n">name_id</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">key</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">xmlsec</span><span class="o">.</span><span class="n">initialize</span><span class="p">()</span>

            <span class="c"># Load the private key</span>
            <span class="n">mngr</span> <span class="o">=</span> <span class="n">xmlsec</span><span class="o">.</span><span class="n">KeysMngr</span><span class="p">()</span>
            <span class="n">key</span> <span class="o">=</span> <span class="n">OneLogin_Saml2_Utils</span><span class="o">.</span><span class="n">format_cert</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">heads</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
            <span class="n">file_key</span> <span class="o">=</span> <span class="n">OneLogin_Saml2_Utils</span><span class="o">.</span><span class="n">write_temp_file</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
            <span class="n">key_data</span> <span class="o">=</span> <span class="n">xmlsec</span><span class="o">.</span><span class="n">Key</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">file_key</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">xmlsec</span><span class="o">.</span><span class="n">KeyDataFormatPem</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
            <span class="n">key_data</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">key_name</span> <span class="o">=</span> <span class="n">basename</span><span class="p">(</span><span class="n">file_key</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
            <span class="n">mngr</span><span class="o">.</span><span class="n">addKey</span><span class="p">(</span><span class="n">key_data</span><span class="p">)</span>
            <span class="n">file_key</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

            <span class="c"># Prepare for encryption</span>
            <span class="n">enc_data</span> <span class="o">=</span> <span class="n">EncData</span><span class="p">(</span><span class="n">xmlsec</span><span class="o">.</span><span class="n">TransformAes128Cbc</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="n">xmlsec</span><span class="o">.</span><span class="n">TypeEncElement</span><span class="p">)</span>
            <span class="n">enc_data</span><span class="o">.</span><span class="n">ensureCipherValue</span><span class="p">()</span>
            <span class="n">key_info</span> <span class="o">=</span> <span class="n">enc_data</span><span class="o">.</span><span class="n">ensureKeyInfo</span><span class="p">()</span>
            <span class="n">enc_key</span> <span class="o">=</span> <span class="n">key_info</span><span class="o">.</span><span class="n">addEncryptedKey</span><span class="p">(</span><span class="n">xmlsec</span><span class="o">.</span><span class="n">TransformRsaPkcs1</span><span class="p">)</span>
            <span class="n">enc_key</span><span class="o">.</span><span class="n">ensureCipherValue</span><span class="p">()</span>
            <span class="n">enc_key_info</span> <span class="o">=</span> <span class="n">enc_key</span><span class="o">.</span><span class="n">ensureKeyInfo</span><span class="p">()</span>
            <span class="n">enc_key_info</span><span class="o">.</span><span class="n">addKeyName</span><span class="p">(</span><span class="n">key_name</span><span class="p">)</span>

            <span class="c"># Encrypt!</span>
            <span class="n">enc_ctx</span> <span class="o">=</span> <span class="n">xmlsec</span><span class="o">.</span><span class="n">EncCtx</span><span class="p">(</span><span class="n">mngr</span><span class="p">)</span>
            <span class="n">enc_ctx</span><span class="o">.</span><span class="n">enc_key</span> <span class="o">=</span> <span class="n">xmlsec</span><span class="o">.</span><span class="n">Key</span><span class="o">.</span><span class="n">generate</span><span class="p">(</span><span class="n">xmlsec</span><span class="o">.</span><span class="n">KeyDataAes</span><span class="p">,</span> <span class="mi">128</span><span class="p">,</span> <span class="n">xmlsec</span><span class="o">.</span><span class="n">KeyDataTypeSession</span><span class="p">)</span>
            <span class="n">ed</span> <span class="o">=</span> <span class="n">enc_ctx</span><span class="o">.</span><span class="n">encryptXml</span><span class="p">(</span><span class="n">enc_data</span><span class="p">,</span> <span class="n">doc</span><span class="o">.</span><span class="n">getroot</span><span class="p">())</span>

            <span class="c"># Build XML with encrypted data</span>
            <span class="n">newdoc</span> <span class="o">=</span> <span class="n">Document</span><span class="p">()</span>
            <span class="n">encrypted_id</span> <span class="o">=</span> <span class="n">newdoc</span><span class="o">.</span><span class="n">createElement</span><span class="p">(</span><span class="s">&#39;saml:EncryptedID&#39;</span><span class="p">)</span>
            <span class="n">newdoc</span><span class="o">.</span><span class="n">appendChild</span><span class="p">(</span><span class="n">encrypted_id</span><span class="p">)</span>
            <span class="n">encrypted_id</span><span class="o">.</span><span class="n">appendChild</span><span class="p">(</span><span class="n">encrypted_id</span><span class="o">.</span><span class="n">ownerDocument</span><span class="o">.</span><span class="n">importNode</span><span class="p">(</span><span class="n">ed</span><span class="p">,</span> <span class="bp">True</span><span class="p">))</span>

            <span class="k">return</span> <span class="n">newdoc</span><span class="o">.</span><span class="n">saveXML</span><span class="p">(</span><span class="n">encrypted_id</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">doc</span><span class="o">.</span><span class="n">saveXML</span><span class="p">(</span><span class="n">name_id</span><span class="p">)</span>
</div>
    <span class="nd">@staticmethod</span>
<div class="viewcode-block" id="OneLogin_Saml2_Utils.get_status"><a class="viewcode-back" href="../../saml2.html#saml2.utils.OneLogin_Saml2_Utils.get_status">[docs]</a>    <span class="k">def</span> <span class="nf">get_status</span><span class="p">(</span><span class="n">dom</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Gets Status from a Response.</span>

<span class="sd">        :param dom: The Response as XML</span>
<span class="sd">        :type: Document</span>

<span class="sd">        :returns: The Status, an array with the code and a message.</span>
<span class="sd">        :rtype: dict</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">status</span> <span class="o">=</span> <span class="p">{}</span>

        <span class="n">status_entry</span> <span class="o">=</span> <span class="n">OneLogin_Saml2_Utils</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">dom</span><span class="p">,</span> <span class="s">&#39;/samlp:Response/samlp:Status&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">status_entry</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s">&#39;Missing Status on response&#39;</span><span class="p">)</span>

        <span class="n">code_entry</span> <span class="o">=</span> <span class="n">OneLogin_Saml2_Utils</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">dom</span><span class="p">,</span> <span class="s">&#39;/samlp:Response/samlp:Status/samlp:StatusCode&#39;</span><span class="p">,</span> <span class="n">status_entry</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">code_entry</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s">&#39;Missing Status Code on response&#39;</span><span class="p">)</span>
        <span class="n">code</span> <span class="o">=</span> <span class="n">code_entry</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">values</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">status</span><span class="p">[</span><span class="s">&#39;code&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">code</span>

        <span class="n">message_entry</span> <span class="o">=</span> <span class="n">OneLogin_Saml2_Utils</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">dom</span><span class="p">,</span> <span class="s">&#39;/samlp:Response/samlp:Status/samlp:StatusMessage&#39;</span><span class="p">,</span> <span class="n">status_entry</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">message_entry</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">status</span><span class="p">[</span><span class="s">&#39;msg&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s">&#39;&#39;</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">status</span><span class="p">[</span><span class="s">&#39;msg&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">message_entry</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">text</span>

        <span class="k">return</span> <span class="n">status</span>
</div>
    <span class="nd">@staticmethod</span>
<div class="viewcode-block" id="OneLogin_Saml2_Utils.decrypt_element"><a class="viewcode-back" href="../../saml2.html#saml2.utils.OneLogin_Saml2_Utils.decrypt_element">[docs]</a>    <span class="k">def</span> <span class="nf">decrypt_element</span><span class="p">(</span><span class="n">encrypted_data</span><span class="p">,</span> <span class="n">enc_ctx</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Decrypts an encrypted element.</span>

<span class="sd">        :param encrypted_data: The encrypted data.</span>
<span class="sd">        :type: DOMElement</span>

<span class="sd">        :param enc_ctx: The encryption context.</span>
<span class="sd">        :type: Encryption Context</span>

<span class="sd">        :returns: The decrypted element.</span>
<span class="sd">        :rtype: DOMElement</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">encrypted_data</span><span class="p">,</span> <span class="n">Element</span><span class="p">):</span>
            <span class="c"># Minidom element</span>
            <span class="n">encrypted_data</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">fromstring</span><span class="p">(</span><span class="n">encrypted_data</span><span class="o">.</span><span class="n">toxml</span><span class="p">())</span>

        <span class="n">decrypted</span> <span class="o">=</span> <span class="n">enc_ctx</span><span class="o">.</span><span class="n">decrypt</span><span class="p">(</span><span class="n">encrypted_data</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">decrypted</span><span class="p">,</span> <span class="n">ElementBase</span><span class="p">):</span>
            <span class="c"># lxml element, decrypted xml data</span>
            <span class="k">return</span> <span class="n">tostring</span><span class="p">(</span><span class="n">decrypted</span><span class="o">.</span><span class="n">getroottree</span><span class="p">())</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c"># decrypted binary data</span>
            <span class="k">return</span> <span class="n">decrypted</span>
</div>
    <span class="nd">@staticmethod</span>
<div class="viewcode-block" id="OneLogin_Saml2_Utils.write_temp_file"><a class="viewcode-back" href="../../saml2.html#saml2.utils.OneLogin_Saml2_Utils.write_temp_file">[docs]</a>    <span class="k">def</span> <span class="nf">write_temp_file</span><span class="p">(</span><span class="n">content</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Writes some content into a temporary file and returns it.</span>

<span class="sd">        :param content: The file content</span>
<span class="sd">        :type: string</span>

<span class="sd">        :returns: The temporary file</span>
<span class="sd">        :rtype: file-like object</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">f</span> <span class="o">=</span> <span class="n">NamedTemporaryFile</span><span class="p">(</span><span class="n">delete</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
        <span class="n">f</span><span class="o">.</span><span class="n">file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">content</span><span class="p">)</span>
        <span class="n">f</span><span class="o">.</span><span class="n">file</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">f</span>
</div>
    <span class="nd">@staticmethod</span>
<div class="viewcode-block" id="OneLogin_Saml2_Utils.add_sign"><a class="viewcode-back" href="../../saml2.html#saml2.utils.OneLogin_Saml2_Utils.add_sign">[docs]</a>    <span class="k">def</span> <span class="nf">add_sign</span><span class="p">(</span><span class="n">xml</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">cert</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Adds signature key and senders certificate to an element (Message or</span>
<span class="sd">        Assertion).</span>

<span class="sd">        :param xml: The element we should sign</span>
<span class="sd">        :type: string | Document</span>

<span class="sd">        :param key: The private key</span>
<span class="sd">        :type: string</span>

<span class="sd">        :param cert: The public</span>
<span class="sd">        :type: string</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">xml</span><span class="p">,</span> <span class="n">Document</span><span class="p">):</span>
            <span class="n">dom</span> <span class="o">=</span> <span class="n">xml</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">xml</span> <span class="o">==</span> <span class="s">&#39;&#39;</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s">&#39;Empty string supplied as input&#39;</span><span class="p">)</span>

            <span class="k">try</span><span class="p">:</span>
                <span class="n">dom</span> <span class="o">=</span> <span class="n">parseString</span><span class="p">(</span><span class="n">xml</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s">&#39;Error parsing xml string&#39;</span><span class="p">)</span>

        <span class="n">xmlsec</span><span class="o">.</span><span class="n">initialize</span><span class="p">()</span>

        <span class="c"># TODO the key and cert could be file descriptors instead</span>
        <span class="c"># Load the private key.</span>
        <span class="n">file_key</span> <span class="o">=</span> <span class="n">OneLogin_Saml2_Utils</span><span class="o">.</span><span class="n">write_temp_file</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
        <span class="n">sign_key</span> <span class="o">=</span> <span class="n">xmlsec</span><span class="o">.</span><span class="n">Key</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">file_key</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">xmlsec</span><span class="o">.</span><span class="n">KeyDataFormatPem</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
        <span class="n">file_key</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
        <span class="c"># Add the certificate to the signature.</span>
        <span class="n">file_cert</span> <span class="o">=</span> <span class="n">OneLogin_Saml2_Utils</span><span class="o">.</span><span class="n">write_temp_file</span><span class="p">(</span><span class="n">cert</span><span class="p">)</span>
        <span class="n">sign_key</span><span class="o">.</span><span class="n">loadCert</span><span class="p">(</span><span class="n">file_cert</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">xmlsec</span><span class="o">.</span><span class="n">KeyDataFormatPem</span><span class="p">)</span>
        <span class="n">file_cert</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

        <span class="c"># Get the EntityDescriptor node we should sign.</span>
        <span class="n">root_node</span> <span class="o">=</span> <span class="n">dom</span><span class="o">.</span><span class="n">firstChild</span>

        <span class="c"># Sign the metadata with our private key.</span>
        <span class="n">signature</span> <span class="o">=</span> <span class="n">Signature</span><span class="p">(</span><span class="n">xmlsec</span><span class="o">.</span><span class="n">TransformExclC14N</span><span class="p">,</span> <span class="n">xmlsec</span><span class="o">.</span><span class="n">TransformRsaSha1</span><span class="p">)</span>
        <span class="n">ref</span> <span class="o">=</span> <span class="n">signature</span><span class="o">.</span><span class="n">addReference</span><span class="p">(</span><span class="n">xmlsec</span><span class="o">.</span><span class="n">TransformSha1</span><span class="p">)</span>
        <span class="n">ref</span><span class="o">.</span><span class="n">addTransform</span><span class="p">(</span><span class="n">xmlsec</span><span class="o">.</span><span class="n">TransformEnveloped</span><span class="p">)</span>

        <span class="n">key_info</span> <span class="o">=</span> <span class="n">signature</span><span class="o">.</span><span class="n">ensureKeyInfo</span><span class="p">()</span>
        <span class="n">key_info</span><span class="o">.</span><span class="n">addX509Data</span><span class="p">()</span>

        <span class="n">dsig_ctx</span> <span class="o">=</span> <span class="n">xmlsec</span><span class="o">.</span><span class="n">DSigCtx</span><span class="p">()</span>
        <span class="n">dsig_ctx</span><span class="o">.</span><span class="n">signKey</span> <span class="o">=</span> <span class="n">sign_key</span>
        <span class="n">dsig_ctx</span><span class="o">.</span><span class="n">sign</span><span class="p">(</span><span class="n">signature</span><span class="p">)</span>

        <span class="n">signature</span> <span class="o">=</span> <span class="n">tostring</span><span class="p">(</span><span class="n">signature</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;ns0:&#39;</span><span class="p">,</span> <span class="s">&#39;ds:&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;:ns0&#39;</span><span class="p">,</span> <span class="s">&#39;:ds&#39;</span><span class="p">)</span>
        <span class="n">signature</span> <span class="o">=</span> <span class="n">parseString</span><span class="p">(</span><span class="n">signature</span><span class="p">)</span><span class="o">.</span><span class="n">firstChild</span>

        <span class="n">insert_before</span> <span class="o">=</span> <span class="n">root_node</span><span class="o">.</span><span class="n">getElementsByTagName</span><span class="p">(</span><span class="s">&#39;saml:Issuer&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">insert_before</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">insert_before</span> <span class="o">=</span> <span class="n">insert_before</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">nextSibling</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">insert_before</span> <span class="o">=</span> <span class="n">root_node</span><span class="o">.</span><span class="n">firstChild</span><span class="o">.</span><span class="n">nextSibling</span><span class="o">.</span><span class="n">nextSibling</span>
        <span class="n">root_node</span><span class="o">.</span><span class="n">insertBefore</span><span class="p">(</span><span class="n">signature</span><span class="p">,</span> <span class="n">insert_before</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">dom</span><span class="o">.</span><span class="n">toxml</span><span class="p">()</span>
</div>
    <span class="nd">@staticmethod</span>
<div class="viewcode-block" id="OneLogin_Saml2_Utils.validate_sign"><a class="viewcode-back" href="../../saml2.html#saml2.utils.OneLogin_Saml2_Utils.validate_sign">[docs]</a>    <span class="k">def</span> <span class="nf">validate_sign</span><span class="p">(</span><span class="n">xml</span><span class="p">,</span> <span class="n">cert</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">fingerprint</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Validates a signature (Message or Assertion).</span>

<span class="sd">        :param xml: The element we should validate</span>
<span class="sd">        :type: string | Document</span>

<span class="sd">        :param cert: The pubic cert</span>
<span class="sd">        :type: string</span>

<span class="sd">        :param fingerprint: The fingerprint of the public cert</span>
<span class="sd">        :type: string</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">xml</span><span class="p">,</span> <span class="n">Document</span><span class="p">):</span>
            <span class="n">dom</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">fromstring</span><span class="p">(</span><span class="n">xml</span><span class="o">.</span><span class="n">toxml</span><span class="p">())</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">xml</span> <span class="o">==</span> <span class="s">&#39;&#39;</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s">&#39;Empty string supplied as input&#39;</span><span class="p">)</span>

            <span class="k">try</span><span class="p">:</span>
                <span class="n">dom</span> <span class="o">=</span> <span class="n">etree</span><span class="o">.</span><span class="n">fromstring</span><span class="p">(</span><span class="n">xml</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s">&#39;Error parsing xml string&#39;</span><span class="p">)</span>

        <span class="n">xmlsec</span><span class="o">.</span><span class="n">initialize</span><span class="p">()</span>

        <span class="c"># Find signature in the dom</span>
        <span class="n">signature_node</span> <span class="o">=</span> <span class="n">OneLogin_Saml2_Utils</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">dom</span><span class="p">,</span> <span class="s">&#39;ds:Signature&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>

        <span class="c"># Prepare context and load cert into it</span>
        <span class="n">dsig_ctx</span> <span class="o">=</span> <span class="n">xmlsec</span><span class="o">.</span><span class="n">DSigCtx</span><span class="p">()</span>
        <span class="n">sign_cert</span> <span class="o">=</span> <span class="n">X509</span><span class="o">.</span><span class="n">load_cert_string</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">cert</span><span class="p">),</span> <span class="n">X509</span><span class="o">.</span><span class="n">FORMAT_PEM</span><span class="p">)</span>
        <span class="n">pub_key</span> <span class="o">=</span> <span class="n">sign_cert</span><span class="o">.</span><span class="n">get_pubkey</span><span class="p">()</span><span class="o">.</span><span class="n">get_rsa</span><span class="p">()</span>
        <span class="n">sign_key</span> <span class="o">=</span> <span class="n">xmlsec</span><span class="o">.</span><span class="n">Key</span><span class="o">.</span><span class="n">loadMemory</span><span class="p">(</span><span class="n">pub_key</span><span class="o">.</span><span class="n">as_pem</span><span class="p">(</span><span class="n">cipher</span><span class="o">=</span><span class="bp">None</span><span class="p">),</span>
                                         <span class="n">xmlsec</span><span class="o">.</span><span class="n">KeyDataFormatPem</span><span class="p">)</span>
        <span class="n">dsig_ctx</span><span class="o">.</span><span class="n">signKey</span> <span class="o">=</span> <span class="n">sign_key</span>

        <span class="c"># Verify signature</span>
        <span class="n">dsig_ctx</span><span class="o">.</span><span class="n">verify</span><span class="p">(</span><span class="n">signature_node</span><span class="p">)</span></div></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Class Index"
             >modules</a> |</li>
        <li><a href="../../index.html">SAML Python library classes and methods</a> &raquo;</li>
          <li><a href="../index.html" >Class code</a> &raquo;</li>
      </ul>
    </div>
    <div class="footer">
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>
  </body>
</html>
